Imports System.ComponentModel
Imports System.Web.HttpContext

<DataObject(True)> _
Public Class InstrumentODS


#Region "For Instrument"

    <DataObjectMethod(DataObjectMethodType.Insert, True)> _
    Public Shared Function CreateInstrument( _
    ByVal InstrumentID As String, _
    ByVal Name As String) As Instrument
        If Current.User.IsInRole("QuestionnaireManagers") _
        OrElse Current.User.IsInRole("QuestionnaireAdmins") Then
            Return QuestionnaireODS.Provider.CreateInstrument(InstrumentID, Name)
        Else
            Throw New Exception("You have no right to create an instrument.")
        End If
    End Function

    <DataObjectMethod(DataObjectMethodType.Delete, True)> _
    Public Shared Function DeleteInstrument(ByVal instrumentId As String) As Boolean
        If Current.User.IsInRole("QuestionnaireAdmins") Then
            Return QuestionnaireODS.Provider.DeleteInstrument(instrumentId)
        Else
            Throw New Exception("You have no right to delete an instrument.")
        End If
    End Function

    <DataObjectMethod(DataObjectMethodType.Select, True)> _
    Public Shared Function GetInstruments( _
    ByVal FieldToSearch As String, _
    ByVal StringToSearch As String) As DataView
        Return QuestionnaireODS.Provider.GetInstruments(FieldToSearch, StringToSearch)
    End Function

    <DataObjectMethod(DataObjectMethodType.Select, False)> _
    Public Shared Function GetInstrument(ByVal instrumentId As String) As Instrument
        Return QuestionnaireODS.Provider.GetInstrument(instrumentId)
    End Function

    'Public Shared Function GetInstrumentDataSet(ByVal id As Integer) As DataSet
    '    Return InstrumentODS.Provider.GetInstrumentDataSet(id)
    'End Function

    'Public Shared Function GetQuestionnaireName(ByVal id As Integer) As String
    '    Return InstrumentODS.Provider.GetQuestionnaire(id).Name
    'End Function

    <DataObjectMethod(DataObjectMethodType.Update, True)> _
    Public Shared Sub UpdateInstrument( _
    ByVal instrumentId As String, _
    ByVal name As String, _
    ByVal description As String, _
    ByVal instruction As String, _
    ByVal original_instrumentId As String)
        If Current.User.IsInRole("QuestionnaireManagers") _
        OrElse Current.User.IsInRole("QuestionnaireAdmins") Then
            Dim inst As New Instrument(instrumentId.Trim, name.Trim)
            inst.Description = description
            inst.Instruction = instruction
            QuestionnaireODS.Provider.UpdateInstrument(inst, original_instrumentId)
        Else
            Throw New Exception("You have no right to update an instrument.")
        End If
    End Sub

    Public Shared Sub LockInstrument( _
    ByVal instrumentId As String)
        QuestionnaireODS.Provider.LockInstrument(instrumentId)
    End Sub

#End Region


#Region "For Instrument & Questions"

    Public Shared Function AddInstrumentQuestion(ByVal instrumentId As String, ByVal questionID As Integer) As Boolean
        If Current.User.IsInRole("QuestionnaireManagers") _
        OrElse Current.User.IsInRole("QuestionnaireAdmins") Then
            Return QuestionnaireODS.Provider.AddInstrumentQuestion(instrumentId, questionID)
        Else
            Throw New Exception("You have no right to add the instrument a question.")
        End If
    End Function

    Public Shared Function DeleteInstrumentQuestion(ByVal uid As String) As Boolean
        If Current.User.IsInRole("QuestionnaireManagers") _
        OrElse Current.User.IsInRole("QuestionnaireAdmins") Then
            Return QuestionnaireODS.Provider.DeleteInstrumentQuestion(uid)
        Else
            Throw New Exception("You have no right to delete the instrument a question.")
        End If
    End Function

    <DataObjectMethod(DataObjectMethodType.Select, False)> _
    Public Shared Function GetInstrumentQuestions(ByVal instrumentId As String) As DataView
        Return QuestionnaireODS.Provider.GetInstrumentQuestions(instrumentId)
    End Function

    Public Shared Sub UpdateInstrumentQuestion(ByVal uid As String, ByVal questionID As Integer)
        If Current.User.IsInRole("QuestionnaireManagers") _
        OrElse Current.User.IsInRole("QuestionnaireAdmins") Then
            QuestionnaireODS.Provider.UpdateInstrumentQuestion(uid, questionID)
        Else
            Throw New Exception("You have no right to update the instrument a question.")
        End If
    End Sub


    Public Shared Function AddInstrumentLogic(ByVal parentUid As String, ByVal questionId As Integer, ByVal logic As String) As Boolean
        If Current.User.IsInRole("QuestionnaireManagers") _
        OrElse Current.User.IsInRole("QuestionnaireAdmins") Then
            Return QuestionnaireODS.Provider.AddInstrumentLogic(parentUid, questionId, logic)
        Else
            Throw New Exception("You have no right to add the instrument a logic.")
        End If
    End Function

    Public Shared Function DeleteInstrumentLogic(ByVal instrumentId As String) As Boolean
        If Current.User.IsInRole("QuestionnaireManagers") _
        OrElse Current.User.IsInRole("QuestionnaireAdmins") Then
            Return QuestionnaireODS.Provider.DeleteInstrument(instrumentId)
        Else
            Throw New Exception("You have no right to delete the instrument a logic.")
        End If
    End Function

    <DataObjectMethod(DataObjectMethodType.Select, False)> _
    Public Shared Function GetInstrumentLogics(ByVal parentUID As String) As DataView
        Return QuestionnaireODS.Provider.GetInstrumentLogics(parentUID)
    End Function

    Public Shared Function GetInstrumentLogic(ByVal uid As String) As Logic
        Return QuestionnaireODS.Provider.GetInstrumentLogic(uid)
    End Function

    Public Shared Sub UpdateInstrumentLogic(ByVal uid As String, ByVal questionId As Integer, ByVal logic As String)
        If Current.User.IsInRole("QuestionnaireManagers") _
        OrElse Current.User.IsInRole("QuestionnaireAdmins") Then
            QuestionnaireODS.Provider.UpdateInstrumentLogic(uid, questionId, logic)
        Else
            Throw New Exception("You have no right to update the instrument a logic.")
        End If
    End Sub

#End Region


End Class
